import Mock from 'mockjs'

// 模拟剧本列表数据
const dramaList = Mock.mock({
  'list|10-20': [{
    'id|+1': 1,
    'name': '@ctitle(5, 20)',
    'cover': '@image("400x300", "#4A7BF7", "#fff", "Drama")',
    'publishTime': '@datetime',
    'status|1': ['published', 'draft'],
    'progress|1': ['0', '50', '100'],
    'author': '@cname',
    'description': '@cparagraph(1, 3)'
  }]
}).list

// 生成默认分集数据
const generateDefaultEpisodes = (dramaId) => {
  const episodes = []
  
  for (let i = 1; i <= 5; i++) {
    episodes.push({
      id: i,
      dramaId,
      name: `第${i}集`,
      status: i % 2 === 0 ? 'confirmed' : 'unconfirmed',
      createdAt: Mock.Random.datetime(),
      updatedAt: Mock.Random.datetime(),
      description: Mock.Random.cparagraph(1, 3),
      storyboard: {
        status: i % 2 === 0 ? 'confirmed' : 'unconfirmed',
        items: Array.from({ length: 5 }).map((_, idx) => ({
          id: idx + 1,
          sceneName: `${i}-${idx + 1} 场景`,
          sceneDesc: Mock.Random.csentence(10, 20)
        }))
      },
      gallery: {
        status: i % 2 === 0 ? 'confirmed' : 'unconfirmed',
        images: Array.from({ length: 8 }).map((_, idx) => ({
          id: idx + 1,
          sceneImageUrl: `https://picsum.photos/400/225?random=${i * 10 + idx}`,
          sceneImagePrompt: Mock.Random.csentence(10, 20)
        }))
      }
    })
  }
  
  return episodes
}

// 剧本列表
Mock.mock(/\/api\/drama\/list/, 'get', (options) => {
  const { url } = options
  const params = new URLSearchParams(url.split('?')[1])
  const page = parseInt(params.get('page')) || 1
  const limit = parseInt(params.get('limit')) || 10
  
  const startIndex = (page - 1) * limit
  const endIndex = startIndex + limit
  
  const pageList = dramaList.slice(startIndex, endIndex)
  
  return {
    code: 200,
    message: 'success',
    data: {
      list: pageList,
      total: dramaList.length,
      page,
      limit
    }
  }
})

// 剧本详情
Mock.mock(/\/api\/drama\/detail\/\d+/, 'get', (options) => {
  const id = parseInt(options.url.match(/\/api\/drama\/detail\/(\d+)/)[1])
  const drama = dramaList.find(item => item.id === id)
  
  if (!drama) {
    return {
      code: 404,
      message: '剧本不存在',
      data: null
    }
  }
  
  return {
    code: 200,
    message: 'success',
    data: drama
  }
})

// 创建剧本
Mock.mock(/\/api\/drama\/create/, 'post', (options) => {
  const formData = JSON.parse(options.body)
  
  const newDrama = {
    id: dramaList.length + 1,
    name: formData.name,
    cover: formData.cover || Mock.Random.image('400x300', '#4A7BF7', '#fff', 'Drama'),
    publishTime: null,
    status: 'draft',
    progress: '0',
    author: formData.author || 'Admin',
    description: formData.description || ''
  }
  
  dramaList.unshift(newDrama)
  
  return {
    code: 200,
    message: '剧本创建成功',
    data: newDrama
  }
})

// 更新剧本
Mock.mock(/\/api\/drama\/update/, 'put', (options) => {
  const updateData = JSON.parse(options.body)
  const index = dramaList.findIndex(item => item.id === updateData.id)
  
  if (index === -1) {
    return {
      code: 404,
      message: '剧本不存在',
      data: null
    }
  }
  
  const updatedDrama = {
    ...dramaList[index],
    ...updateData,
    updatedAt: new Date().toISOString()
  }
  
  dramaList.splice(index, 1, updatedDrama)
  
  return {
    code: 200,
    message: '剧本更新成功',
    data: updatedDrama
  }
})

// 删除剧本
Mock.mock(/\/api\/drama\/delete\/\d+/, 'delete', (options) => {
  const id = parseInt(options.url.match(/\/api\/drama\/delete\/(\d+)/)[1])
  const index = dramaList.findIndex(item => item.id === id)
  
  if (index === -1) {
    return {
      code: 404,
      message: '剧本不存在',
      data: null
    }
  }
  
  dramaList.splice(index, 1)
  
  return {
    code: 200,
    message: '剧本删除成功',
    data: null
  }
})

// 发布剧本
Mock.mock(/\/api\/drama\/publish\/\d+/, 'put', (options) => {
  const id = parseInt(options.url.match(/\/api\/drama\/publish\/(\d+)/)[1])
  const drama = dramaList.find(item => item.id === id)
  
  if (!drama) {
    return {
      code: 404,
      message: '剧本不存在',
      data: null
    }
  }
  
  drama.status = 'published'
  drama.publishTime = new Date().toISOString()
  
  return {
    code: 200,
    message: '剧本发布成功',
    data: drama
  }
})

// 取消发布剧本
Mock.mock(/\/api\/drama\/unpublish\/\d+/, 'put', (options) => {
  const id = parseInt(options.url.match(/\/api\/drama\/unpublish\/(\d+)/)[1])
  const drama = dramaList.find(item => item.id === id)
  
  if (!drama) {
    return {
      code: 404,
      message: '剧本不存在',
      data: null
    }
  }
  
  drama.status = 'draft'
  
  return {
    code: 200,
    message: '剧本取消发布成功',
    data: drama
  }
})

// 获取分集列表
Mock.mock(/\/api\/drama\/episodes\/\d+/, 'get', (options) => {
  const dramaId = parseInt(options.url.match(/\/api\/drama\/episodes\/(\d+)/)[1])
  const drama = dramaList.find(item => item.id === dramaId)
  
  if (!drama) {
    return {
      code: 404,
      message: '剧本不存在',
      data: null
    }
  }
  
  // 生成该剧本的分集数据
  const episodes = generateDefaultEpisodes(dramaId)
  
  return {
    code: 200,
    message: 'success',
    data: episodes
  }
})

// 更新分集
Mock.mock(/\/api\/drama\/episodes\/\d+\/\d+/, 'put', (options) => {
  const matches = options.url.match(/\/api\/drama\/episodes\/(\d+)\/(\d+)/)
  const dramaId = parseInt(matches[1])
  const episodeId = parseInt(matches[2])
  const updateData = JSON.parse(options.body)
  
  return {
    code: 200,
    message: '分集更新成功',
    data: {
      id: episodeId,
      dramaId,
      ...updateData,
      updatedAt: new Date().toISOString()
    }
  }
})

// 创建分集
Mock.mock(/\/api\/drama\/episodes\/\d+$/, 'post', (options) => {
  const dramaId = parseInt(options.url.match(/\/api\/drama\/episodes\/(\d+)/)[1])
  const episodeData = JSON.parse(options.body)
  
  const newEpisode = {
    id: Date.now(),
    dramaId,
    ...episodeData,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  }
  
  return {
    code: 200,
    message: '分集创建成功',
    data: newEpisode
  }
})

// 删除分集
Mock.mock(/\/api\/drama\/episodes\/\d+\/\d+/, 'delete', (options) => {
  return {
    code: 200,
    message: '分集删除成功',
    data: null
  }
}) 